package com.xueyu.util;

import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class itpython {
    public static void main(String[] args) throws IOException, ParseException {
        ExecutorService executor = Executors.newFixedThreadPool(1);
        // 提交任务给线程池
        executor.submit(new Task("Task 1"));
//        executor.submit(new Task("Task 2"));
//        executor.submit(new Task("Task 3"));
        // 关闭线程池
        executor.shutdown();
    }
    static class Task implements Runnable {
        private String name;

        public Task(String name) {
            this.name = name;
        }

        @Override
        public void run() {
            System.out.println("Executing task: " + name);
//            for (int i = 0; i<1000; i++){
                CloseableHttpClient httpClient = HttpClients.createDefault();
//                String url = "https://www.linovelib.com/novel/75/11093.html";
                String url = "https://www.qianyege.com/12/12307/5804446.html";
//                String url = "https://wap.biqupe.com/book_21570_13116746_2.html";
                HttpGet httpGet = new HttpGet(url);
                CloseableHttpResponse response = null;
                try {
                    response = httpClient.execute(httpGet);
                    int code = response.getCode();
                    System.out.println(code);
                    if (code == 200){
                        HttpEntity httpEntity = response.getEntity();
                        String content = EntityUtils.toString(httpEntity, "GBK");
                        Document doc = Jsoup.parse(content);
//                         Element gettextID = doc.getElementById("TextContent");
                        Element gettextID = doc.getElementById("content");
                        // Elements text = gettextID.getElementsByTag("p");
                        // String textp = text.text();
                        // System.out.println(textp);
                        System.out.println("——————————————————————————————————————————————————————————————————");
                        // System.out.println(text);
                        String filtrationText = gettextID.text();

                        // 过滤获取的文本参数
                        String nbsp = "&nbsp;";
                        String cc = "&nbsp";
                        String br = "<br>";
                        String div = "<div>";
                        String divI = "</div>";
                        String full = "。";
                        String question = "？";
                        String point = "！";
                        String marks = "”";

                        String filtration = filtrationText.replace(nbsp, " ")
                                .replace(cc, "cc")
                                .replace(br, " ")
                                .replace(div, "")
                                .replace(divI, "")
                                .replace(full, "\n")
                                .replace(question, "\n")
                                .replace(point, "\n")
                                .replace(marks, "\n");
                        //打印获取到的值
                        System.out.println(filtration);

                        String path = "D:\\exploitTool\\com.XM.exTCode\\IntelliJ IDEA 2023.1-File\\systeam\\src\\main\\java\\com\\xueyu";
                        int i = 1;
                        File file = new File(path + "\\Book" + "\\book" + i + ".txt");
                        while (file.exists()) {
                            i++;
                            file = new File(path + "\\book" + i + ".txt");
                        }

                        try (FileOutputStream fos = new FileOutputStream(file)) {
                            byte[] bytes = filtration.getBytes();
                            fos.write(bytes);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                        // 关闭
                        response.close();
                        httpClient.close();
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
//            }
        }
    }
}
